/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package de.jadehs.cosima.impl;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

import de.jadehs.cosima.CosimaPackage;
import de.jadehs.cosima.Instruction;
import de.jadehs.cosima.State;
import de.jadehs.cosima.Transition;
import de.jadehs.cosima.util.PrettyPrintUtil;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Transition</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link de.jadehs.cosima.impl.TransitionImpl#getStart <em>Start</em>}</li>
 *   <li>{@link de.jadehs.cosima.impl.TransitionImpl#getEnd <em>End</em>}</li>
 *   <li>{@link de.jadehs.cosima.impl.TransitionImpl#getStartInstructions <em>Start Instructions</em>}</li>
 *   <li>{@link de.jadehs.cosima.impl.TransitionImpl#getEndInstructions <em>End Instructions</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
/**
 * @author erdal
 * 
 */
public class TransitionImpl extends EObjectImpl implements Transition {
	/**
	 * The cached value of the '{@link #getStart() <em>Start</em>}' reference.
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getStart()
	 * @generated
	 * @ordered
	 */
	protected State start;

	/**
	 * The cached value of the '{@link #getEnd() <em>End</em>}' reference. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getEnd()
	 * @generated
	 * @ordered
	 */
	protected State end;

	/**
	 * The cached value of the '{@link #getStartInstructions()
	 * <em>Start Instructions</em>}' containment reference list. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getStartInstructions()
	 * @generated
	 * @ordered
	 */
	protected EList<Instruction> startInstructions;

	/**
	 * The cached value of the '{@link #getEndInstructions()
	 * <em>End Instructions</em>}' containment reference list. <!--
	 * begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @see #getEndInstructions()
	 * @generated
	 * @ordered
	 */
	protected EList<Instruction> endInstructions;

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	protected TransitionImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return CosimaPackage.Literals.TRANSITION;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public State getStart() {
		if (start != null && start.eIsProxy()) {
			InternalEObject oldStart = (InternalEObject) start;
			start = (State) eResolveProxy(oldStart);
			if (start != oldStart) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE,
							CosimaPackage.TRANSITION__START, oldStart, start));
			}
		}
		return start;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public State basicGetStart() {
		return start;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public void setStart(State newStart) {
		State oldStart = start;
		start = newStart;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET,
					CosimaPackage.TRANSITION__START, oldStart, start));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public State getEnd() {
		if (end != null && end.eIsProxy()) {
			InternalEObject oldEnd = (InternalEObject) end;
			end = (State) eResolveProxy(oldEnd);
			if (end != oldEnd) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE,
							CosimaPackage.TRANSITION__END, oldEnd, end));
			}
		}
		return end;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public State basicGetEnd() {
		return end;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public void setEnd(State newEnd) {
		State oldEnd = end;
		end = newEnd;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET,
					CosimaPackage.TRANSITION__END, oldEnd, end));
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public EList<Instruction> getStartInstructions() {
		if (startInstructions == null) {
			startInstructions = new EObjectContainmentEList<Instruction>(
					Instruction.class, this,
					CosimaPackage.TRANSITION__START_INSTRUCTIONS);
		}
		return startInstructions;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	public EList<Instruction> getEndInstructions() {
		if (endInstructions == null) {
			endInstructions = new EObjectContainmentEList<Instruction>(
					Instruction.class, this,
					CosimaPackage.TRANSITION__END_INSTRUCTIONS);
		}
		return endInstructions;
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd,
			int featureID, NotificationChain msgs) {
		switch (featureID) {
		case CosimaPackage.TRANSITION__START_INSTRUCTIONS:
			return ((InternalEList<?>) getStartInstructions()).basicRemove(
					otherEnd, msgs);
		case CosimaPackage.TRANSITION__END_INSTRUCTIONS:
			return ((InternalEList<?>) getEndInstructions()).basicRemove(
					otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
		case CosimaPackage.TRANSITION__START:
			if (resolve)
				return getStart();
			return basicGetStart();
		case CosimaPackage.TRANSITION__END:
			if (resolve)
				return getEnd();
			return basicGetEnd();
		case CosimaPackage.TRANSITION__START_INSTRUCTIONS:
			return getStartInstructions();
		case CosimaPackage.TRANSITION__END_INSTRUCTIONS:
			return getEndInstructions();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
		case CosimaPackage.TRANSITION__START:
			setStart((State) newValue);
			return;
		case CosimaPackage.TRANSITION__END:
			setEnd((State) newValue);
			return;
		case CosimaPackage.TRANSITION__START_INSTRUCTIONS:
			getStartInstructions().clear();
			getStartInstructions().addAll(
					(Collection<? extends Instruction>) newValue);
			return;
		case CosimaPackage.TRANSITION__END_INSTRUCTIONS:
			getEndInstructions().clear();
			getEndInstructions().addAll(
					(Collection<? extends Instruction>) newValue);
			return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
		case CosimaPackage.TRANSITION__START:
			setStart((State) null);
			return;
		case CosimaPackage.TRANSITION__END:
			setEnd((State) null);
			return;
		case CosimaPackage.TRANSITION__START_INSTRUCTIONS:
			getStartInstructions().clear();
			return;
		case CosimaPackage.TRANSITION__END_INSTRUCTIONS:
			getEndInstructions().clear();
			return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
		case CosimaPackage.TRANSITION__START:
			return start != null;
		case CosimaPackage.TRANSITION__END:
			return end != null;
		case CosimaPackage.TRANSITION__START_INSTRUCTIONS:
			return startInstructions != null && !startInstructions.isEmpty();
		case CosimaPackage.TRANSITION__END_INSTRUCTIONS:
			return endInstructions != null && !endInstructions.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * @generated NOT
	 */
	@Override
	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append(getStart().getName())
				.append(" -> ")
				.append(getEnd().getName())
				.append(": ")
				.append(PrettyPrintUtil
						.prettyPrintInstructions(getStartInstructions()))
				.append(" <> ")
				.append(PrettyPrintUtil
						.prettyPrintInstructions(getEndInstructions()));
		return buf.toString();
	}
} // TransitionImpl
